#include <stdlib.h>
#include <stdio.h>

#include "E2V_CAM_HelpLib.h"

#define OPENGL_GP_BUILD_STATICLIB
#include "opengl_gp_lib.h"

int main(void) {
	int err = 0;

	//unsigned short ia[] = { 1,2,3,4,5,6,7,8,9 };
	//unsigned short ib[] = { 1,2,3,4,5,6,7,8,9 };
	//E2V_ResampleImageEveryRow1(&err, ia, ib, 1000, 2048, 0, 1090, 1370);

	printf("InitializeLibrary : %s \n", E2V_InitializeLibrary(&err));

	err = 0;
	printf("OpenCamera : %s \n", E2V_OpenCamera(&err,0));

	printf("Opened Camera UID : %s \n", E2V_GetCameraUID());

	//err = 0;
	//printf("OpenCamera : %s \n", E2V_OpenCameraByUID(&err, "313834344137343031"));

	char buf[256] = { 0 };
	int cnt = 256;
	//User defined name
	cnt = 64;
	err = 0;
	printf("WriteRegister 0x0184 (User defined name): %s \n", E2V_WriteRegister(&err, 0x0184, "UserId",&cnt));

	
	cnt = 256;
	err = 0;
	printf("ReadRegister : %s \n", E2V_ReadRegister(&err, 0x0184, buf, &cnt));
	printf("Camera Name : %s \n", buf);

	int ivalue = 0;
	//Circular Buffer 
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x4F000018 (Circular Buffer on/off) : %s \n", E2V_WriteRegister(&err, 0x4F000018, &ivalue, &cnt));


	ivalue = 0;
	//Buffer Type : FIFO
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x4F00001C (Buffer Type FIFO/LIFO): %s \n", E2V_WriteRegister(&err, 0x4F00001C, &ivalue, &cnt));

	ivalue = 2048;
	//Image width: (must be multiple of 8 pixels)
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x20030 (Image width): %s \n", E2V_WriteRegister(&err, 0x20030, &ivalue, &cnt));

	ivalue = 4;
	//Output mode : (4: Pixel 12-bit)
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x12020 (Output mode): %s \n", E2V_WriteRegister(&err, 0x12020, &ivalue, &cnt));

	ivalue = 4000;
	//Line period : 0~ 65535 ( (655,35us). 1 step = 0.01us)
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x12100 (Line period): %s \n", E2V_WriteRegister(&err, 0x12100, &ivalue, &cnt));

	ivalue = 400;
	//Exposure time : 0~ 65535 ( (655,35us). 1 step = 0.01us)
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x12108 (Exposure time): %s \n", E2V_WriteRegister(&err, 0x12108, &ivalue, &cnt));


	ivalue = 0;
	//Trigger modes 
	// 0 : Internal line trigger with programmable exposure time and line period 
	// 1 : Internal line trigger with max exposure time and programmable line period 
	// 2 : External line trigger with programmable exposure time 
	// 3 : External line trigger with maximum exposure time 
	// 4 : External mixed line and frame trigger with programmable exposure time 
	// 5 : External mixed line and frame trigger with maximum exposure time 
	// 6 : External frame trigger and internal line trigger
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x1210C (Trigger modes): %s \n", E2V_WriteRegister(&err, 0x1210C, &ivalue, &cnt));

	ivalue = 0;
	//GPI format : 
	//0: TTL / RS422 / RS485(120 Ohms termination disable) 
	//1 : LVDS(120 Ohms termination enable)
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x12120 (GPI format): %s \n", E2V_WriteRegister(&err, 0x12120, &ivalue, &cnt));

	ivalue = 0;
	//Analog gain : 0=x1.0, 1=x2.0, 2=x4.0
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x12200 (Analog gain): %s \n", E2V_WriteRegister(&err, 0x12200, &ivalue, &cnt));

	ivalue = 0;
	//Digital gain : Digital gain from x1.0 (0) to x7.9961 (1791) 1 step = x0.0039
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x12204 (Digital gain): %s \n", E2V_WriteRegister(&err, 0x12204, &ivalue, &cnt));

	ivalue = 0;
	//Metadata enable : enable/disable
	cnt = 4;
	err = 0;
	printf("WriteRegister 0x4F000000 (Metadata enable): %s \n", E2V_WriteRegister(&err, 0x4F000000, &ivalue, &cnt));

	err = 0;
	printf("SetImageParameters : %s \n", E2V_SetImageParameters(&err, 1, 1));

	////
	err = 0;
	printf("FlushBuffers : %s \n", E2V_FlushBuffers(&err));

	err = 0;
	printf("StartAcquisition : %s \n", E2V_StartAcquisition(&err));

	size_t img_size = 1 * 2048 * sizeof(unsigned short);
	unsigned short *dstImgPtr = (unsigned short *)malloc(img_size);
	
	err = 0;
	printf("RetrieveImage : %s \n", E2V_RetrieveImage(&err, dstImgPtr, 1, 2048, 1, 60));

	err = 0;
	printf("FlushBuffers : %s \n", E2V_FlushBuffers(&err));

	err = 0;
	printf("RetrieveImage : %s \n", E2V_RetrieveImage(&err, dstImgPtr, 1, 2048, 1, 60));
	
	if (!err) {
		printf("retrieve image data : \n");
		for (int i = 0; i < 2048; i++)
		{
			printf("%d, ", dstImgPtr[i]);
		}
		printf("\n");
	}

	free(dstImgPtr);
	dstImgPtr = NULL;

	err = 0;
	printf("StopAcquisition : %s \n", E2V_StopAcquisition(&err));
	////

	err = 0;
	printf("CloseCamera : %s \n", E2V_CloseCamera(&err));

	err = 0;
	printf("TerminateLibrary : %s \n", E2V_TerminateLibrary(&err));


	printf("--------------------- Opengl GPUGP test ---------------------\n");
	glgpInitContext();
	err = 0;
	printf("glaccInit : %s \n", glaccInit(&err));
	printf("OpengGL Version : %s \n", glaccGetGLVersionInfo());
	glgpReleaseContext();

	//printf("hello world!\n");
	system("pause");
}